import { useUserStore } from '../stores/user'

// 权限指令
export const permission = {
    mounted(el, binding) {
        const userStore = useUserStore()
        const { value } = binding

        if (value) {
            // 检查角色权限
            if (typeof value === 'string') {
                // 单个角色检查
                if (!userStore.hasRole(value)) {
                    el.style.display = 'none'
                }
            } else if (Array.isArray(value)) {
                // 多个角色检查（任一角色满足即可）
                const hasAnyRole = value.some(role => userStore.hasRole(role))
                if (!hasAnyRole) {
                    el.style.display = 'none'
                }
            } else if (typeof value === 'object') {
                // 复杂权限检查
                const { roles, permissions } = value

                let hasAccess = true

                // 检查角色
                if (roles) {
                    const requiredRoles = Array.isArray(roles) ? roles : [roles]
                    const hasRole = requiredRoles.some(role => userStore.hasRole(role))
                    if (!hasRole) {
                        hasAccess = false
                    }
                }

                // 检查权限
                if (permissions && hasAccess) {
                    const requiredPermissions = Array.isArray(permissions) ? permissions : [permissions]
                    const hasPermission = requiredPermissions.every(permission => userStore.hasPermission(permission))
                    if (!hasPermission) {
                        hasAccess = false
                    }
                }

                if (!hasAccess) {
                    el.style.display = 'none'
                }
            }
        }
    },

    updated(el, binding) {
        // 当权限发生变化时重新检查
        const userStore = useUserStore()
        const { value } = binding

        if (value) {
            el.style.display = 'block' // 先显示

            if (typeof value === 'string') {
                if (!userStore.hasRole(value)) {
                    el.style.display = 'none'
                }
            } else if (Array.isArray(value)) {
                const hasAnyRole = value.some(role => userStore.hasRole(role))
                if (!hasAnyRole) {
                    el.style.display = 'none'
                }
            } else if (typeof value === 'object') {
                const { roles, permissions } = value

                let hasAccess = true

                if (roles) {
                    const requiredRoles = Array.isArray(roles) ? roles : [roles]
                    const hasRole = requiredRoles.some(role => userStore.hasRole(role))
                    if (!hasRole) {
                        hasAccess = false
                    }
                }

                if (permissions && hasAccess) {
                    const requiredPermissions = Array.isArray(permissions) ? permissions : [permissions]
                    const hasPermission = requiredPermissions.every(permission => userStore.hasPermission(permission))
                    if (!hasPermission) {
                        hasAccess = false
                    }
                }

                if (!hasAccess) {
                    el.style.display = 'none'
                }
            }
        }
    }
} 